package com.ts.code.creator.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class FileUtil {

    /**
     * 获取服务列表
     * @param projectFilePath 项目路径
     * @return 列表
     */
    public static List<String> listServices(String projectFilePath) {
        File projectDir = new File(projectFilePath);
        return Arrays.stream(projectDir.listFiles())
                .filter(t -> !t.getName().startsWith("."))
                .map(File::getName)
                .collect(Collectors.toList());
    }

    public static boolean exist(String filePath) {
        File file = new File(filePath);
        return file.exists();
    }

    public static void writeTo(String path, String content) {
        File file = new File(path);
        if (!file.exists()) {
            try {
                FileOutputStream fos = new FileOutputStream(file);
                fos.write(content.getBytes("utf-8"));
                fos.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
